package com.soilflow.web.controller.reception.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soilflow.common.constant.Constants;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.common.core.domain.model.LoginBody;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.ColorfulUser;
import com.soilflow.common.domain.SysArticle;
import com.soilflow.common.domain.SysCollect;
import com.soilflow.common.domain.SysLike;
import com.soilflow.common.utils.SecurityUtils;
import com.soilflow.framework.web.service.SysLoginService;
import com.soilflow.info.service.ISysCollectService;
import com.soilflow.info.service.ISysLikeService;
import com.soilflow.system.mapper.ColorfulUserMapper;
import com.soilflow.system.service.IColorfulUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/users")
public class UserController extends BaseController {
    @Autowired
    private ISysLikeService likeService;
    @Autowired
    private ISysCollectService collectService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private IColorfulUserService colorfulUserService;
    /**
     * 登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String username = loginBody.getUsername()+"_Duocai";
        System.out.println("loginBody.getUsername() = " + loginBody.getUsername());
        // 生成令牌
        String token = loginService.login(username, loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        System.out.println("token = " + token);
        return ajax;
    }
    /**
     * 获取当前用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }


    /**
     * 展示当前用户点赞数据
     */
    @GetMapping("/getLike")
    public TableDataInfo getLike(){
        startPage();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        System.out.println("userId = " + userId);
        LambdaQueryWrapper<SysLike> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysLike::getCreateId,userId);
        List<SysLike> list = likeService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 展示当前用户收藏数据
     */
    @GetMapping("/getCollect")
    public TableDataInfo getCollect(){
        startPage();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysCollect::getCreateId,userId);
        List<SysCollect> list = collectService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 修改个人信息
     */
    @PutMapping
    public AjaxResult update(ColorfulUser user){
            return toAjax(colorfulUserService.updateById(user));
    }

}
